//
//  NSMutableAttributedString+QTAdd.m
//  QTCategory
//
//  Created by 张俊博 on 2017/8/23.
//  Copyright © 2017年 CISDI. All rights reserved.
//

#import "NSMutableAttributedString+QTAdd.h"
#import "NSBundle+QTAdd.h"

@implementation NSMutableAttributedString (QTAdd)

- (NSArray<NSTextCheckingResult *> *)emojiMatches
{
    static NSRegularExpression *regex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSBundle *bundle = [NSBundle QTResourceBundle].zh_HansBundle;
        NSString *emojiPlist = [bundle pathForResource:@"EmojiList"
                                                ofType:@"plist"];
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:emojiPlist];
        NSMutableArray *allEmojis = [NSMutableArray array];
        [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            [allEmojis addObjectsFromArray:obj];
        }];
        NSString *regulaStr = [NSString stringWithFormat:@"(%@)", [allEmojis componentsJoinedByString:@"|"]];
        NSError *error;
        regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                          options:NSRegularExpressionCaseInsensitive
                                                            error:&error];
    });
    
    return [regex matchesInString:self.string options:0 range:NSMakeRange(0, [self length])];
}

@end
